package cn.source.system.controller;

import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.domain.entity.SysUser;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.enums.BusinessType;
import cn.source.common.utils.SecurityUtils;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.system.service.IDeyuSysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户信息Controller
 *
 * @author deyucare.com
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/deyu/deyuUser")
public class DeyuSysUserController extends BaseController
{
    @Autowired
    private IDeyuSysUserService sysUserService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = sysUserService.selectDeyuSysUserList(sysUser);
        return getDataTable(list);
    }

    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(sysUserService.selectSysUserByUserId(userId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUser sysUser)
    {
        SysUser deyuSysUser = new SysUser();
        deyuSysUser.setUserName(sysUser.getUserName());
        deyuSysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        deyuSysUser.setNickName(sysUser.getNickName());
        deyuSysUser.setPhonenumber(sysUser.getPhonenumber());
        deyuSysUser.setEmail(sysUser.getEmail());
        deyuSysUser.setSex(sysUser.getSex());
        deyuSysUser.setUserType("00");
        Long[] postIds = new Long[1];
        postIds[0] = 9L;
        Long[] roleIds = new Long[1];
        roleIds[0] = 10L;
        deyuSysUser.setPostIds(postIds);
        deyuSysUser.setRoleIds(roleIds);
        //  保存完用户后，还需要设置用户的角色，部门与岗位
        return toAjax(sysUserService.insertUser(deyuSysUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUser sysUser)
    {
        return toAjax(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(sysUserService.deleteUserByIds(userIds));
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUser sysUser)
    {
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('deyu:deyuUser:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(sysUserService.resetPwd(user));
    }
}
